package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.powernode.entity.UmsRoleResource;
import com.powernode.mapper.UmsRoleResourceMapper;
import com.powernode.service.UmsRoleResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色关联资源 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2022-04-11
 */
@Service
public class UmsRoleResourceServiceImpl extends ServiceImpl<UmsRoleResourceMapper, UmsRoleResource> implements UmsRoleResourceService {

    @Transactional
    public Boolean save(Long roleId, Long[] menus, Long[] btns) {
        UpdateWrapper<UmsRoleResource> wrapper = new UpdateWrapper<UmsRoleResource>();
        wrapper.eq("role_id",roleId);
        this.remove(wrapper);
        List<UmsRoleResource> list = new ArrayList<UmsRoleResource>();
        if (menus != null){
            for (Long menuId : menus) {
                list.add(new UmsRoleResource(roleId,menuId,1));
            }
        }
        if (btns != null){
            for (Long btnId : btns) {
                list.add(new UmsRoleResource(roleId,btnId,0));
            }
        }
        return this.saveBatch(list);
    }

    public List<UmsRoleResource> getByRoleId(Long roleId) {
        QueryWrapper<UmsRoleResource> wrapper = new QueryWrapper<UmsRoleResource>();
        wrapper.eq("role_id",roleId);
        return this.list(wrapper);
    }
}
